package com.example.mylibrary;

import android.app.Instrumentation;
import android.graphics.Rect;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.Configurator;
import androidx.test.uiautomator.UiCollection;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class Cashbook {
    private static final String TAG = "简账";
    static Instrumentation instrumentation = null;
    static UiDevice device = null;

    @BeforeClass
    public static void initDevice() {
        Log.i(TAG, "连接设备");
        instrumentation =
                InstrumentationRegistry.getInstrumentation();
        device = UiDevice.getInstance(instrumentation);
        device.registerWatcher
                ("call", new CallWatcher(device));
        device.runWatchers();
        Configurator configurator = Configurator.getInstance();
        configurator.setWaitForSelectorTimeout(3000);
        device.pressHome();
    }

    @Before
    public void open() throws InterruptedException {
        device.findObject
                (By.text("简账")).click();
        Thread.sleep(3000);
    }
    @Test
    // 简账—注册—注册成功
    public void test01() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/register")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_account")).setText("zzl");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw")).setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw_again")).setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/register")).click();
    }
    @Test
    // 简账—登录—登录失败(账号不存在)
    public void test02() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_account")).setText("1");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw")).setText("2");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/login")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—登录—登录失败(密码为空)
    public void test03() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_account")).setText("1");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw")).setText("");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/login")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—登录—登录失败(账号为空)
    public void test04() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_account")).setText("");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw")).setText("2");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/login")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—登录—登录失败(密码错误)
    public void test05() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_account")).setText("zzl");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw")).setText("2");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/login")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—注册—注册失败(两次输入密码不一致)
    public void test06() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/register")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_account")).setText("zzl");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw")).setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw_again")).setText("1234567");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/register")).click();
    }
    @Test
    // 简账—注册—注册失败(忘记输入密码)
    public void test07() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/register")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_account")).setText("zzl");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw")).setText("");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw_again")).setText("");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/register")).click();
    }
    @Test
    // 简账—注册—注册失败(忘记输入账号)
    public void test08() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/register")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_account")).setText("");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw")).setText("");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw_again")).setText("");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/register")).click();
    }
    @Test
    // 简账—注册—已有账号返回登录
    public void test09() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/register")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/back")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_account")).setText("zzl");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_psw")).setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/login")).click();
        Thread.sleep(1000);
    }
    @Test
   // 简账—跳转到图表页面
    public void test10() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/tb")).click();
        Thread.sleep(1000);
    }


    @Test
    // 简账—跳转到我的页面
    public void test11() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/wd")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—搜索所有“支出”,并返回
    public void test12() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/mx")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/search")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_search")).setText("支出");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_search")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_back")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—搜索所有“收入”,并返回
    public void test13() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/mx")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/search")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_search")).setText("收入");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_search")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_back")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—搜索所有“2022-05-16”那天的账单,并返回
    public void test14() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/mx")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/search")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/et_search")).setText("2022-05-16");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_search")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_back")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—图表—查看支出
    public void test15() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/tb")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_zc")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—图表—查看收入
    public void test16() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/tb")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_sr")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—添加话费支出10元
    public void test17() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/fb")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"支出\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"话费\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_1")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_0")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_sure")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—添加购物支出10元
    public void test18() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/fb")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"支出\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"购物\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_1")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_0")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_sure")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—添加吃饭支出10元
    public void test19() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/fb")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"支出\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"吃饭\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_1")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_0")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_sure")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—添加工资收入10元
    public void test20() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/fb")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"收入\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"工资\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_1")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_0")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_sure")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—添加津贴收入10元
    public void test21() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/fb")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"收入\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"津贴\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_1")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_0")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_sure")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—添加其他收入10元
    public void test22() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/fb")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"收入\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"其他\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_1")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_0")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_sure")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—添加红包收入10元，并备注“这是红包收入”
    public void test23() throws InterruptedException {
        device.findObject(By.res("com.lyc.cashbook:id/fb")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"收入\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"红包\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_1")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_0")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"添加备注信息\"")).setText("这是红包收入");
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/btn_sure")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—查看菜单中“关于”信息
    public void test24() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/mx")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"更多选项\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"关于\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"确定\"")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—直接在“明细”模块更换背景
    public void test25() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/mx")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"更多选项\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"换背景\"")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—在“我的”模块更换背景，观察“明细”模块背景是否改变
    public void test26() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"我的\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"更多选项\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"换背景\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"明细\"")).click();
        Thread.sleep(1000);
    }
    @Test
    // 简账—明细—开启背景音乐
    public void test27() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:id/mx")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"更多选项\"")).click();
        Thread.sleep(1000);
        device.findObject(By.res("com.lyc.cashbook:text/\"关闭/开启音乐\"")).click();
        Thread.sleep(1000);
    }


    @After
    public void quit() throws InterruptedException {
        device.pressHome();
        Thread.sleep(3000);

    }

    @AfterClass
    public static void tearDown() {
        device.removeWatcher("call");
    }
}

